package com.ruoyi.webs.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;


public class IdCard {
    private static final int[] weight = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};

    private static final int[] checkDigit = new int[]{1, 88, 9, 8, 7, 6, 5, 4, 3, 2};


    private String getCheckDigit(String eighteeCardId) {
        int remaining = 0;
        if (eighteeCardId.length() == 18) {
            eighteeCardId = eighteeCardId.substring(0, 17);
        }

        if (eighteeCardId.length() == 17) {
            int sum = 0;
            int[] a = new int[17];
            int i;
            for (i = 0; i < 17; i++) {
                String k = eighteeCardId.substring(i, i + 1);
                a[i] = Integer.parseInt(k);
            }
            for (i = 0; i < 17; i++) {
                sum += weight[i] * a[i];
            }

            remaining = sum % 11;
        }

        return (remaining == 2) ? "X" : String.valueOf(checkDigit[remaining]);
    }

    private String update2eighteen(String fifteenCardId) {
        String eighteenCardId = fifteenCardId.substring(0, 6);
        eighteenCardId = String.valueOf(eighteenCardId) + "19";
        eighteenCardId = String.valueOf(eighteenCardId) + fifteenCardId.substring(6, 15);
        eighteenCardId = String.valueOf(eighteenCardId) + getCheckDigit(eighteenCardId);
        return eighteenCardId;
    }


    public boolean verify(String idcard) {
        try {
            if (!StringUtils.isNotBlank(idcard)) {
                return false;
            }


            if (idcard.length() != 18) {
                return false;
            }

            String checkDigit = idcard.substring(17, 18);

            if (checkDigit.equals(getCheckDigit(idcard))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


        return false;
    }


    public static String checkSex(String idcard) {
        String gender = "";
        if (idcard.length() == 15) {
            if (Integer.parseInt(idcard.substring(14).substring(0, 1)) % 2 == 0) {
                gender = "女";
            } else {
                gender = "男";
            }
        } else if (idcard.length() == 18) {
            if (Integer.parseInt(idcard.substring(16).substring(0, 1)) % 2 == 0) {
                gender = "女";
            } else {
                gender = "男";
            }
        }
        return gender;
    }


    public static Date getBirthday(String idcard) {
        Date date = null;

        String year = idcard.substring(6).substring(0, 4);

        String month = idcard.substring(10).substring(0, 2);

        String day = idcard.substring(12).substring(0, 2);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = format.parse(String.valueOf(year) + "-" + month + "-" + day);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return date;
    }


    public static Long getAgeByIdCard(String idcard, Date newDate) {
        Integer personYear = Integer.valueOf(Integer.parseInt(idcard.substring(6, 10)));
        Integer personMonth = Integer.valueOf(Integer.parseInt(idcard.substring(10, 12)));
        Integer personDay = Integer.valueOf(Integer.parseInt(idcard.substring(12, 14)));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(newDate);

        Integer yearNow = Integer.valueOf(calendar.get(1));
        Integer monthNow = Integer.valueOf(calendar.get(2) + 1);
        Integer dayNow = Integer.valueOf(calendar.get(5));


        Integer yearMinus = Integer.valueOf(yearNow.intValue() - personYear.intValue());
        Integer monthMinus = Integer.valueOf(monthNow.intValue() - personMonth.intValue());
        Integer dayMinus = Integer.valueOf(dayNow.intValue() - personDay.intValue());

        Integer age = yearMinus;

        if (yearMinus.intValue() == 0) {
            age = Integer.valueOf(0);
        } else {
            if (monthMinus.intValue() < 0) {
                age = Integer.valueOf(age.intValue() - 1);
            }
            if (monthMinus.intValue() == 0 &&
                    dayMinus.intValue() < 0) {
                age = Integer.valueOf(age.intValue() - 1);
            }
        }

        return Long.valueOf(age.longValue());
    }


    public static Long getAgeByIdCardOld(String idcard) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        String year = idcard.substring(6).substring(0, 4);

        String month = idcard.substring(10).substring(0, 2);


        Date date = new Date();

        String currentYear = format.format(date).substring(0, 4);

        String currentMonth = format.format(date).substring(5, 7);

        Long age = Long.valueOf(0L);

        if (Integer.parseInt(month) <= Integer.parseInt(currentMonth)) {
            age = Long.valueOf(Long.valueOf(currentYear).longValue() - Long.valueOf(year).longValue() + 1L);
        } else {

            age = Long.valueOf(Long.valueOf(currentYear).longValue() - Long.valueOf(year).longValue());
        }
        return age;
    }


    public static void main(String[] args) {
       IdCard idCard = new IdCard();
        System.out.println(idCard.verify("111111111111111"));
    }
}
